﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Core.DataAccess;
using SonCa.SocialNetwork.Web.ASCX.IPresenters;
using SonCa.SocialNetwork.Web.ASCX.Presenters;
using SonCa.SocialNetwork.Web.HelperClass;
using Core.Common;
using Core.Interfaces;
using System.ComponentModel.Composition;
using System.Web.UI.HtmlControls;

namespace SonCa.SocialNetwork.Web.ASCX
{
    public partial class ProfileDisplay : System.Web.UI.UserControl, IProfileDisplay
    {
        private ProfileDisplayPresenter _presenter;
        [Import]
        private IUserSession _userSession;

        protected void Page_Load(object sender, EventArgs e)
        {
            MEFManager.Compose(this);
            _presenter = new ProfileDisplayPresenter();
            _presenter.Init(this);
        }

        public void LoadDisplay(FriendsProfile contentObj, int accountID)
        {
            lblFriend.Text = contentObj.FullName;
            linkToPersonal_name.NavigateUrl = "../Personals/PersonalPage.aspx?otherAccountID=" + contentObj.AccountID;
            imgLevel.ImageUrl = "../img/" + contentObj.Level;            
            lblScore.Text = contentObj.Score != -1 ? contentObj.Score.ToString() : "";
            
            lblStatus.Text = contentObj.Status;
            imgAvatar.ImageUrl = "~/Images/ProfileAvatar/ProfileImage.aspx?AccountID=" + contentObj.AccountID.ToString();
            linkToPersonal_img.NavigateUrl = "../Personals/PersonalPage.aspx?otherAccountID=" + contentObj.AccountID;
            if (contentObj.IsFriend)            
                ibInviteFriend.Visible = false;            
            else
            {
                if (contentObj.WaitingAccept)
                    ibInviteFriend.ImageUrl = "../img/requestsended.png";
                else
                    ibInviteFriend.Visible = true;
            }

            lblNumberUnlike.Text = contentObj.UnlikeCount.ToString();
            lblNumberLike.Text = contentObj.LikeCount.ToString();
            ibInviteFriend.Attributes.Add("class", contentObj.AccountID.ToString());
            lbtLike.Attributes.Add("class", contentObj.AccountID.ToString() + ";" + contentObj.StatusID.ToString());
            lbtUnlike.Attributes.Add("class", contentObj.AccountID.ToString() + ";" + contentObj.StatusID.ToString());
            if (contentObj.AccountID == accountID)            
                lbtShare.Visible = false;            
            else
            {
                lbtShare.Attributes.Add("class", contentObj.AccountID.ToString() + ";" + contentObj.StatusID.ToString());
                lbtShare.Visible = true;
            }
            rptComment.DataSource = new List<FriendsProfile> { contentObj };
            rptComment.DataBind();            
            if (contentObj.FileSystemFolderID == 1) // Photo
            {
                rptPhotos.DataSource = !string.IsNullOrEmpty(contentObj.FileID) ? Translator.UploadObjectToHCFile(new List<FriendsProfile> { contentObj }) : new List<HCFile> { };
                rptPhotos.DataBind();
            }
            else if (contentObj.FileSystemFolderID == 2)    // Video
            {
                //string folder = DateTime.Now.Year.ToString() + DateTime.Now.Month.ToString();
                video_source_1.Attributes.Add("src", "../Files/Videos/" + contentObj.FileSystemName + "__O.mp4");
                video_source_2.Attributes.Add("src", "../Files/Videos/" + contentObj.FileSystemName + "__O.webm");
                video_source_3.Attributes.Add("src", "../Files/Videos/" + contentObj.FileSystemName + "__O.ogg");
                video_source_4.Attributes.Add("src", "../Files/Videos/" + contentObj.FileSystemName + "__O.swf");                
                video.Attributes.Remove("style");
            }
            else if (contentObj.FileSystemFolderID == 3)    // Audio
            {
                audio_source_1.Attributes.Add("src", "../Files/Audios/" + contentObj.FileSystemName + "__O.mp3");
                audio_source_2.Attributes.Add("src", "../Files/Audios/" + contentObj.FileSystemName + "__O.wav");
                audio_source_3.Attributes.Add("src", "../Files/Audios/" + contentObj.FileSystemName + "__O.ogg");                                                                              
                audio_source_4.Attributes.Add("src", "../Files/Audios/" + contentObj.FileSystemName + "__O.mp3");
                audio.Attributes.Remove("style");
            }
        }

        protected void rptPhotos_ItemDataBound(object sender, RepeaterItemEventArgs e)
        {
            if (e.Item.ItemType == ListItemType.Item)
            {
                HyperLink linkPhotos = e.Item.FindControl("linkPhotos") as HyperLink;
                Literal litFileSystemName = e.Item.FindControl("litFileSystemName") as Literal;
                Literal litFileID = e.Item.FindControl("litFileID") as Literal;
                Literal litExtension = e.Item.FindControl("litExtension") as Literal;
                linkPhotos.ImageUrl =  "~/Files/Photos/" + litFileSystemName.Text + "__s." + litExtension.Text;
            }
        }

        public void LoadPhotos(List<HCFile> HCFiles)
        {
            if (HCFiles.Count > 0)
            {
                rptPhotos.DataSource = HCFiles;
                rptPhotos.DataBind();    
            }            
        }
    }
}